<?php
/**
 * 文章管理控制器
 * User: mrlifang
 * Date: 17/5/7
 * Time: 下午3:19
 */

namespace Admin\Controller;


use Admin\Model\ArticleModel;
use Admin\Model\CategoryModel;
use Frame\Libs\BaseController;
use Frame\Vendor\Pager;

class ArticleController extends BaseController
{

    /*
     * 显示文章列表
     */
    public function index(){



        //获取所有的文章数据
        //$articles = ArticleModel::getInstance()->fetchAll();
        $Categorys = CategoryModel::getInstance()->fetchAll();
        $Categorys = CategoryModel::getInstance()->categoryList($Categorys,0,0);


        //构建搜索条件
        $where = "2>1";
        if(!empty($_REQUEST['category_id'])){
            $where .= " AND category_id=".$_REQUEST['category_id'];
        }
        if(!empty($_REQUEST['keyword'])){
            $where .= " AND title = %{$_REQUEST['keyword']}%";
        }


        //分页参数
        $orderby = "id desc";
        $pagesize = 10;
        $page = isset($_GET['page']) ? $_GET['page'] :1;
        $startrow = ($page-1)*$pagesize;
        $records = ArticleModel::getInstance()->rowCount($where);
        $params = array(
            'c' => CONTROLLER,
            'a' => ACTION,
            'category_id' => "{$_REQUEST['category_id']}"
        );
        if(!empty($_REQUEST['category_id']) ){
            $params['category_id'] = $_REQUEST['category_id'];
        }
        if(!empty($_REQUEST['keyword'])){
            $params['keyword'] = $_REQUEST['keyword'];
        }

        $articles = ArticleModel::getInstance()->
        fetchAllWithJoin($where,$orderby,$startrow,$pagesize);

        //创建分页对象
        $pageObj = new Pager($pagesize,$page,$records,$params);
        //var_dump($pageObj);exit();
        $pageStr = $pageObj->showPageStr();

        $this->smarty->assign(array(
            'articles' => $articles,
            "categorys" => $Categorys,
            "pageStr" => $pageStr
        ));

        $this->smarty->display("Article".DS."index.html");
    }
    /*
     *显示文章添加界面
     */
    public function add(){
        //获取无限极分类数据
        $Categorys = CategoryModel::getInstance()->fetchAll();
        $Categorys = CategoryModel::getInstance()->categoryList($Categorys,0,0);

        //向视图赋值,并显示视图
        $this->smarty->assign("categorys",$Categorys);
        $this->smarty->display("Article".DS."add.html");

    }

    /*
     * 插入文章数据
     */
    public function insert(){
        //获取表单提交值
        $data['category_id'] = $_POST['category_id'];
        $data['title']       = $_POST['title'];
        $data['orderby']     = $_POST['orderby'];
        $data['content']     = $_POST['content'];
        $data['top']         = isset($_POST['top'])?1:0;
        $data['top']         = isset($_POST['top'])?1:0;
        $data['addate']      = time();
        //调用模型类对象insert()方法 写入数据
        ArticleModel::getInstance()->insert($data);
        $this->jump("添加文章内容成功","?c=Article");
    }
    /*
     * 删除文章数据
     */
    public function delete(){
        $id = $_GET['id'];
        ArticleModel::getInstance()->delete($id);
        $this->jump("删除文章成功O(∩_∩)O~~","?c=Article");
    }


}